$(() => {
    const token = GetQueryString("token");
    let page = 1;
    let page_size = 10;
    ajaxList(page);

    // 收藏商品列表
    function ajaxList(page) {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'User|goods_favorite_list', 'p': page, 'page_size': page_size, "token": token },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';
                    data.forEach(item => {
                        // // console.log(item.description)
                        html += `<div class="collectProductLists">`
                        if ($num == 1) {
                            html += `<lable class="collectCheckStyle" style="display: block;">`
                        } else {
                            html += `<lable class="collectCheckStyle">`
                        }
                        if ($numclick == 0) {
                            html += `<input class="checkcell" type="checkbox" value="${item.id}">`
                        } else {
                            html += `<input class="checkcell" type="checkbox" value="${item.id}" checked>`
                        }

                        html += `<i class="collectCheck"></i>
                            </lable>
                            <a href="comp_shopDetail.html?id=${item.id}&token=${token}" class="collectProductList">
                                <img class="colProImg" src="${item.goods_thumb == '' ?　'../img/user.jpg' : item.goods_thumb}" alt="${item.goods_name}">
                                <div class="colProListRight">
                                    <p style="height:1.1rem;overflow:hidden;word-break: break-word;">${item.goods_name}</p>`
                                    if(item.price_show_model == 1){
                                        html +=`<p class="redPrice" style="font-size:.28rem;">¥${item.goods_price}</p>`
                                    }else if(item.price_show_model == 2){
                                        html +=`<p style="font-size:.28rem;"><del style="color:#666;font-size:.24rem;">¥${item.price_before}</del>/<span class="redPrice" style="font-size:.28rem;" >¥${item.goods_price}</span></p>`
                                    }else{
                                        html +=`<p class="redPrice" style="font-size:.28rem;">¥${item.price_area}</p>`
                                    } 
                                html +=`</div>
                            </a>
                        </div>
                        `;
                    });
                    if (data.length < page_size) {
                        $('.notMore').css('display', 'flex');
                    }
                    $('#colllist').append(html);

                    $('.checkcell').on('click', function() {
                        // 全部勾选 全选点亮
                        var checkcell = $('.checkcell');
                        var isChecked = false;
                        for (var i = 0; i < checkcell.length; i++) {
                            if (checkcell.eq(i).prop('checked') == false) {
                                isChecked = true;
                            }
                        }
                        if (isChecked == false) {
                            $('.allChoose').prop('checked', true);
                        } else {
                            $('.allChoose').prop('checked', false);
                        }
                    })
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    }

    //下拉加载更多
    $(window).scroll(function() {
        if ($(document).scrollTop() >= $(document).height() - $(window).height() - 1) {
            // console.log('我到底了');
            if ($('.notMore').css('display') != "flex") {
                page++;
                ajaxList(page);
            }
        }
    })

    $("#checkDel").on('click', function() {
        dialogClose();
        var chechid = $(".checkcell[type='checkbox']:checked").length;
        var chckdel = '';
        if ($(".checkcell").prop('checked') == true) {
            for (i = 0; i < chechid; i++) {
                var checkell = $(".collectProductLists").eq(i).find(".checkcell").val();
                if (chckdel == undefined) {
                    chckdel = '';
                }
                chckdel += checkell + ",";
            }
        }

        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'User|goods_favorite_del', 'goods_id': chckdel, "token": token },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    alert(res.message);
                    $(".checkcell[type='checkbox']:checked").parent().parent().remove();
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })

    });
})